package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.constant.MessageConstant;
import com.health.dao.MenuDao;
import com.health.dao.RoleDao;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Menu;
import com.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname MenuServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/17 17:35
 * @Author RainGrd
 */
@Service(interfaceClass = MenuService.class)
@Transactional
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Menu> queryMenuList(Integer roleId) {
        //根据Id查询
        //根据角色权限和菜单等级查询一级菜单
        List<Menu> menus = menuDao.selectMenuByLevelAndRoleId(roleId, 1);
        //根据当前用户的权限
        for (Menu menu : menus) {
            log.info("id:{}", menu.getId());
            //根据一级菜单的Id和角色Id查询子菜单
            List<Menu> menuList = menuDao.selectMenuByParentMenuIdAndRoleId(menu.getId(),roleId);
            log.info("menuList:{}", menuList);
            //填充二级菜单
            menu.setChildren(menuList);
        }
        log.info("menus:{}", menus);
        return menus;
    }

    @Override
    public Result saveMenu(Menu menu, Integer[] roleIds) {
        log.info("menu:{}", menu);
        try {
            //查询菜单是否重合
            int i = menuDao.insertMenu(menu);
            log.info("menu:{}", menu);
            //插入菜单与角色表
            setMenuIdAndRoleIds(menu.getId(), roleIds);
            return i > 0 ? Result.success(MessageConstant.ADD_CHECKITEM_SUCCESS) : Result.error(MessageConstant.ADD_CHECKITEM_FAIL);
        } catch (Exception e) {
            return Result.error(MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @Override
    public Result queryMenuByLevel() {
        log.info("menuList:{}", menuDao.selectMenuListByLevel());
        return Result.success(menuDao.selectMenuListByLevel());
    }

    @Override
    public PageResult queryMenuPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> menus = menuDao.selectMenuByCondition(queryPageBean.getQueryString());
        return new PageResult(menus.getTotal(), menus.getResult());
    }

    @Override
    public List<Integer> queryRoleIdsByMenuId(Integer menuId) {
        return menuDao.selectRoleIdsByMenuId(menuId);
    }

    @Override
    public Result editMenu(Menu menu, Integer[] roleIds) {
        try {
            //根据检查组id删除中间表数据（清理原有关联关系）
            menuDao.deleteAssociation(menu.getId());
            //重新补充
            setMenuIdAndRoleIds(menu.getId(), roleIds);
            //正式编辑检查组
            int i = menuDao.updateMenu(menu);
            return i > 0 ? Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS) : Result.error(MessageConstant.EDIT_CHECKGROUP_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    @Override
    public Result clearMenu(Integer id) {
        //删除检查组
        try {
            //根据检查组id删除中间表数据（清理原有关联关系）
            menuDao.deleteAssociation(id);
            //删除检查组
            int i = menuDao.deleteMenu(id);
            return i > 0 ? Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS) : Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    /**
     * 用于设置套餐与检查组的关系
     *
     * @param roleIds
     * @param menuId
     */
    private void setMenuIdAndRoleIds(Integer menuId, Integer[] roleIds) {
        //判断检查项目Id数组是否为空
        if (roleIds != null && roleIds.length > 0) {
            //添加检查组和检查项
            for (Integer roleId : roleIds) {
                log.info("roleId:{}", roleId);
                menuDao.insertMenuAndRole(menuId, roleId);
            }
        }
    }
}
